package com.pictureBook.merchant.controller;


import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.pictureBook.merchant.entity.StoreUser;
import com.pictureBook.merchant.service.IStoreUserService;
import com.sun.org.apache.xpath.internal.operations.Bool;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Ding
 * @since 2022-01-07
 */
@RestController
@RequestMapping("/storeUser")
public class StoreUserController {

    @Resource
    private IStoreUserService storeUserService;

    @ApiOperation("会员列表")
    @GetMapping("/getList")
    @ResponseBody
    public JSONObject getList(@RequestParam("so_id") Integer soId,@RequestParam("index") Integer index,@RequestParam("num") Integer num,
                              @RequestParam(name = "nameOrTel",defaultValue = "") String nameOrTel,
                              @RequestParam(name = "expiredDay",defaultValue = "") String expiredDay,
                              @RequestParam(name = "mealId",defaultValue = "") String mealId,
                              @RequestParam(name = "babyName",defaultValue = "") String babyName,
                              @RequestParam(name = "babySex",defaultValue = "") String babySex,
                              @RequestParam(name = "babyAge",defaultValue = "") String babyAge,
                              @RequestParam(name = "babyBirthDay",defaultValue = "") String babyBirthDay){
        JSONObject jsonObject = new JSONObject();
        Map<String,Object> searchText = new HashMap<>();
        searchText.put("nameOrTel",nameOrTel);
        searchText.put("expiredDay",expiredDay);
        searchText.put("mealId",mealId);
        searchText.put("babyName",babyName);
        searchText.put("babySex",babySex);
        searchText.put("babyAge",babyAge);
        searchText.put("babyBirthDay",babyBirthDay);
        System.err.println(searchText);
        PageInfo<StoreUser> storeUsers ;
        try {
            storeUsers = storeUserService.getList(index,num,soId,searchText);
            jsonObject.put("code",200);
            jsonObject.put("msg","获取成功");
            JSONObject jsonTemp = new JSONObject();
            jsonTemp.put("list",storeUsers.getList());
            jsonTemp.put("total",storeUsers.getTotal());
            jsonObject.put("data",jsonTemp);
        }catch (Exception e){
            e.printStackTrace();
            jsonObject.put("code",500);
            jsonObject.put("msg","服务器异常，获取失败");
        }
        return jsonObject;
    }

    @ApiOperation("会员回收站")
    @GetMapping("/getListRecycle")
    @ResponseBody
    public JSONObject getListRecycle(@RequestParam("so_id") Integer soId,@RequestParam("index") Integer index,@RequestParam("num") Integer num){
        JSONObject jsonObject = new JSONObject();

        PageInfo<StoreUser> storeUsers ;
        try {
            storeUsers = storeUserService.getListRecycle(index,num,soId);
            jsonObject.put("code",200);
            jsonObject.put("msg","获取成功");
            JSONObject jsonTemp = new JSONObject();
            jsonTemp.put("list",storeUsers.getList());
            jsonTemp.put("total",storeUsers.getTotal());
            jsonObject.put("data",jsonTemp);
        }catch (Exception e){
            e.printStackTrace();
            jsonObject.put("code",500);
            jsonObject.put("msg","服务器异常，获取失败");
        }
        return jsonObject;
    }

    @ApiOperation("请假会员列表")
    @GetMapping("/getListLeave")
    @ResponseBody
    public JSONObject getListLeave(@RequestParam("so_id") Integer soId,@RequestParam("index") Integer index,@RequestParam("num") Integer num){
        JSONObject jsonObject = new JSONObject();

        PageInfo<StoreUser> storeUsers ;
        try {
            storeUsers = storeUserService.getListLeave(index,num,soId);
            jsonObject.put("code",200);
            jsonObject.put("msg","获取成功");
            JSONObject jsonTemp = new JSONObject();
            jsonTemp.put("list",storeUsers.getList());
            jsonTemp.put("total",storeUsers.getTotal());
            jsonObject.put("data",jsonTemp);
        }catch (Exception e){
            e.printStackTrace();
            jsonObject.put("code",500);
            jsonObject.put("msg","服务器异常，获取失败");
        }
        return jsonObject;
    }
}
